var crypto  = require('crypto')
,   core    = require("core")
,   db      = core.db
,   schema  = db.schema;


var POST = function(req, res, next){
    var body = req.body
    ,   name = body.name
    ,   pwd  = body.pwd + ""
    ,   sha1 = crypto.createHash('sha1');
    sha1.update(pwd);

    pwd = sha1.digest('hex');

    var User = {
        name : name,
        pwd  : pwd
    };

    var Users = new schema.User(User);

    Users.save(function(err, docs){
        if(err){
            var token = res.token({exp: 3000}, {error: "注册失败，请联系管理员"});
            return res.redirect("/admin/register?token="+ token);
        };
        res.session({user: docs.name});
        global.app.user.count += 1;
        return res.redirect("/admin/index");
    });

};

module.exports = POST;
